import wxValidate from 'wechat-validate'

Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    mobile: '',
    description: '',
    appointment: '',
    houseName: '',
    repairItemId: '',
    repairItemName: '',
    houseList: [],
    repairItem: [],
    attachment: [],
    id: '',
    minDate: Date.now(),
    maxDate: Date.now() + 1000 * 3600 * 24 * 3
  },
  rules: {
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' }
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }]
  },
  behaviors: [wxValidate],
  onLoad ({ id }) {
    this.setData({ id })
    this.getHouseList()
    this.getRepairItem()
    this.getRepairPart()
  },
  async getRepairPart () {
    console.log(this.data.id)
    if (!this.data.id) return
    const { data } = await wx.http.get('/repair/' + this.data.id)
    this.setData({
      ...data,
      houseName: data.houseInfo
    })
    console.log(data)
  },
  async getHouseList () {
    const { data } = await wx.http.get('/house')
    this.setData({ houseList: data })
  },
  selectHouseInfo (ev) {
    this.setData({
      houseId: ev.detail.id,
      houseName: ev.detail.name
    })
  },
  async getRepairItem () {
    const { data } = await wx.http.get('/repairItem')
    console.log(data)
    this.setData({
      repairItem: data
    })
  },
  selectRepairItemInfo (ev) {
    this.setData({
      repairItemId: ev.detail.id,
      repairItemName: ev.detail.name
    })
  },
  selectDateInfo (ev) {
    console.log(ev.detail)
    const res = wx.utils.dateFormat(ev.detail)
    this.setData({
      appointment: res,
      dateLayerVisible: false
    })
  },
  uploadPicture (ev) {
    const { file } = ev.detail

    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getApp().token
      },
      success: result => {
        const data = JSON.parse(result.data)

        if (data.code !== 10000) return wx.utils.toast('上传失败')

        const { attachment } = this.data

        attachment.push(data.data)

        this.setData({ attachment })
      }
    })
  },
  openHouseLayer () {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer () {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer () {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer () {
    this.setData({
      repairLayerVisible: false
    })
  },

  openDateLayer () {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer () {
    this.setData({ dateLayerVisible: false })
  },
  goList () {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index'
    })
  },
  async submit () {
    const id = this.data.id
    if (!this.validate()) return
    const { houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    const { code } = id
      ? await wx.http.post('/repair', {
          houseId,
          repairItemId,
          mobile,
          appointment,
          description,
          attachment,
          id
        })
      : await wx.http.post('/repair', {
          houseId,
          repairItemId,
          mobile,
          appointment,
          description,
          attachment
        })
    if (code !== 10000) return wx.utils.toast()
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  }
})
